package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.*;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.mapper.ApplicationRecordMapper;
import com.woniuxy.factory.mapper.EmpMapper;
import com.woniuxy.factory.mapper.ProductMapper;
import com.woniuxy.factory.mapper.ProductionPlanMapper;
import com.woniuxy.factory.service.ProductionPlanService;
import com.woniuxy.factory.util.ApplicationType;
import com.woniuxy.factory.util.AuditType;
import com.woniuxy.factory.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductionPlanServiceImp implements ProductionPlanService {
    @Autowired
    ProductionPlanMapper productionPlanMapper;
    @Autowired
    ApplicationRecordMapper applicationRecordMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    EmpMapper empMapper;

    @Override
    public ResponseResult<List<ProductionPlan>> getProductionPlans(ProductionPlan productionPlan, int page, int limit) {
        //查询计划条数
        int count = productionPlanMapper.getCount(productionPlan);
        //判断计划是否存在
        if (count<1){
            return new ResponseResult<>(AuditType.ALREADY_PASSED,"没有该计划",null,null);
        }
        int home = (page-1)*limit;

        //查询计划列
        List<ProductionPlan> productionPlans=null;
        //异常处理
        try {
            productionPlans=productionPlanMapper.selectProductionPlan(productionPlan,home,limit);
        }catch (RuntimeException e){
            return new ResponseResult<>(AuditType.ALREADY_PASSED,"系统维护中",null,null);
        }
        //遍历判断状态
        for (int i=0;i<productionPlans.size();i++){
            if (productionPlans.get(i).getApplicationRecord().getApplystatus()==0){
                productionPlans.get(i).setPlanState("未通过");
            }else if (productionPlans.get(i).getApplicationRecord().getApplystatus()==1){
                productionPlans.get(i).setPlanState("已通过");
            }
        }
        return new ResponseResult<>(AuditType.No_Approval,"查询成功",count,productionPlans);
    }

    @Override
    @Transactional
    public ResponseResult addProductionPlan(ProductionPlan productionPlan, HttpServletRequest request) {
        //获取当前登陆账户的信息
        EmpUser empUser = (EmpUser) request.getSession().getAttribute("account");
        //根据员工编号查询员工
        System.out.println(empUser);
        Emp emp1 = null;
        try {
            emp1 = empMapper.selectEmpById(empUser.getEmp().getEmpId());
        }catch (NullPointerException e){
            throw new BusinessException("您还未登陆，请前往登陆");
        }
        String prpId = UuidUtil.get16UUID();
        productionPlan.setPrpId(prpId);
        productionPlan.setPlanstaff(emp1.getEmpName());
        //新增计划
        productionPlanMapper.insertproductionPlan(productionPlan);
        //新增计划申请记录
        ApplicationRecord applicationRecord = new ApplicationRecord();
        applicationRecord.setAprId(UuidUtil.get16UUID());
        applicationRecord.setApplicationtype(ApplicationType.PRODUCTIONPLAN);
        applicationRecord.setApplicationnum(prpId);
        System.out.println(applicationRecord);

        try {
            applicationRecordMapper.insertApplicationRecord(applicationRecord);
        }catch (RuntimeException e){
            return new ResponseResult(AuditType.ALREADY_PASSED,"系统维护中",null,null);
        }
        return new ResponseResult(AuditType.No_Approval,"新增成功",null,null);

    }
    //删除计划
    @Override
    public ResponseResult deleteProductionPlan(String prpId) {
        ResponseResult responseResult = new ResponseResult();
        //判断该计划状态
        ProductionPlan productionPlan = new ProductionPlan();
        productionPlan.setPrpId(prpId);
        List<ProductionPlan> productionPlans = productionPlanMapper.selectProductionPlan(productionPlan, 0, 3);
        try {
            if (productionPlans.get(0).getApplicationRecord().getApplystatus()!=0){
                return new ResponseResult(AuditType.ALREADY_PASSED,"计划状态已修改不可删除",null,null);
            }
        }catch (NullPointerException e){
            return new ResponseResult(AuditType.ALREADY_PASSED,"该计划不存在",null,null);
        }
        productionPlanMapper.deleteProductionPlan(prpId);
        return new ResponseResult(AuditType.No_Approval,"删除成功！",null,null);
    }

    @Override
    public ResponseResult insertState(String prpId) {
        //判断该计划状态
        ProductionPlan productionPlan = new ProductionPlan();
        productionPlan.setPrpId(prpId);
        List<ProductionPlan> productionPlans = productionPlanMapper.selectProductionPlan(productionPlan, 0, 3);
        try {
            if (productionPlans.get(0).getApplicationRecord().getApplystatus()!=0){

                return new ResponseResult(AuditType.ALREADY_PASSED,"计划状态已修改不可删除",0,null);
            }
        }catch (NullPointerException e){
            return new ResponseResult(AuditType.ALREADY_PASSED,"该计划不存在",0,null);
        }
        return null;
    }

    @Override
    public ResponseResult<Map> getProductionPlan(String prpId) {
        Map<String,Object> map = new HashMap<>();
        //查询所有商品信息
        List<Product> products = productMapper.selectProducts(new Product());
        //查询指定计划
        ProductionPlan productionPlan = new ProductionPlan();
        productionPlan.setPrpId(prpId);
        List<ProductionPlan> productionPlans = productionPlanMapper.selectProductionPlan(productionPlan, 0, 3);
        ApplicationRecord applicationRecord = new ApplicationRecord();
        applicationRecord.setApplicationnum(productionPlans.get(0).getPrpId());
        List<ApplicationRecord> applicationRecords = applicationRecordMapper.selectApplicationRecordByCondition(applicationRecord);
        if (applicationRecords.get(0).getApplystatus()!=0){
            return new ResponseResult<>(AuditType.ALREADY_PASSED,"计划状态已修改不可删除",0,map);
        }
        map.put("products",products);
        map.put("productionPlan",productionPlans.get(0));
        return new ResponseResult<>(AuditType.No_Approval,"查询成功",0,map);
    }

    @Override
    public ResponseResult updateProductionplan(ProductionPlan productionPlan) {
        ProductionPlan productionPlan1 = new ProductionPlan();
        productionPlan1.setPrpId(productionPlan.getPrpId());
        //查询计划是否存在
        List<ProductionPlan> productionPlans = productionPlanMapper.selectProductionPlan(productionPlan1, 0, 3);
        try {
            if (productionPlans.get(0).getApplicationRecord().getApplystatus()!=0){
                return new ResponseResult(AuditType.ALREADY_PASSED,"计划状态已修改不可删除",null,null);
            }
        }catch (NullPointerException e){
            return new ResponseResult(AuditType.ALREADY_PASSED,"该计划不存在",null,null);
        }
        //修改计划
        productionPlanMapper.updateProductionPlan(productionPlan);
        return new ResponseResult(AuditType.No_Approval,"计划修改成功",null,null);
    }
}
